//
//  NewSendViewController.m
//  AntsExpress
//
//  Created by Home on 14/1/16.
//  Copyright © 2016年 slccper. All rights reserved.
//

#import "NewSendViewController.h"
#import "AntsPickView.h"
@interface NewSendViewController ()<UIAlertViewDelegate>
{
    NSMutableArray *SendProvinceList;
    NSMutableArray *SendCityList;
    NSMutableArray *SendDistrictList;
    NSInteger SendProvinceSelect;
    NSInteger SendCitySelect;
    NSInteger SendDistrictSelect;
    NSMutableArray *GetProvinceList;
    NSMutableArray *GetCityList;
    NSMutableArray *GetDistrictList;
    NSInteger GetProvinceSelect;
    NSInteger GetCitySelect;
    NSInteger GetDistrictSelect;
    AntsPickView *Pickview;
    BOOL isSendProvince;
    BOOL isGetProvince;
    BOOL isSendCity;
    BOOL isGetCity;
    BOOL isSendDistrict;
    BOOL isGetDistrict;
}
@property (weak, nonatomic) IBOutlet UIButton *Sure;
@property (weak, nonatomic) IBOutlet UIButton *Cancel;
@property (weak, nonatomic) IBOutlet UITextField *send;
@property (weak, nonatomic) IBOutlet UITextField *sendNo;
@property (weak, nonatomic) IBOutlet UITextField *sendAdd;
@property (weak, nonatomic) IBOutlet UITextField *get;
@property (weak, nonatomic) IBOutlet UITextField *getNo;
@property (weak, nonatomic) IBOutlet UITextField *getAdd;
@property (weak, nonatomic) IBOutlet UITextField *getName;
@property (weak, nonatomic) IBOutlet UIButton *getSure;
@property (weak, nonatomic) IBOutlet UIButton *SendProvince;
@property (weak, nonatomic) IBOutlet UIButton *SendCity;
@property (weak, nonatomic) IBOutlet UIButton *SendDistrict;
@property (weak, nonatomic) IBOutlet UIButton *GetProvince;
@property (weak, nonatomic) IBOutlet UIButton *GetCity;
@property (weak, nonatomic) IBOutlet UIButton *GetDistrict;

@end

@implementation NewSendViewController

- (void)viewDidLoad {
    self.title=@"新增寄件";
    [super viewDidLoad];
    self.Sure.layer.cornerRadius=3;
    self.Sure.layer.borderColor=[UIColor grayColor].CGColor;
    self.Sure.layer.borderWidth=1;
    self.Cancel.layer.cornerRadius=3;
    self.Cancel.layer.borderColor=[UIColor grayColor].CGColor;
    self.Cancel.layer.borderWidth=1;
    self.getSure.layer.cornerRadius=3;
    self.getSure.layer.borderColor=[UIColor grayColor].CGColor;
    self.getSure.layer.borderWidth=1;
    ants = [[clsAntsService alloc]init];
    ants.delegate=self;
    if (self.dic) {
        self.send.text = [self.dic objectForKey:@"MemberName"];
        self.sendNo.text = [self.dic objectForKey:@"MemberTelephone"];
        self.sendAdd.text = [self.dic objectForKey:@"MemberAdress"];
        self.get.text = [self.dic objectForKey:@"SEName"];
        self.getNo.text = [self.dic objectForKey:@"SETelephone"];
        self.getAdd.text = [self.dic objectForKey:@"SEAdress"];
        self.getName.text = [self.dic objectForKey:@"GoodsName"];
        
        self.getSure.selected = [[self.dic objectForKey:@"IsBJ"] integerValue];
        
        [self.SendProvince setTitle:[self.dic objectForKey:@"MProvinceName"] forState:UIControlStateNormal];
        [self.SendCity  setTitle:[self.dic objectForKey:@"MCityName"]forState:UIControlStateNormal];
        [self.SendDistrict  setTitle:[self.dic objectForKey:@"MDistrictName"]forState:UIControlStateNormal];
        [self.GetProvince setTitle:[self.dic objectForKey:@"ProvinceName"]forState:UIControlStateNormal];
        [self.GetCity setTitle:[self.dic objectForKey:@"CityName"]forState:UIControlStateNormal];
        [self.GetDistrict setTitle:[self.dic objectForKey:@"DistrictName"]forState:UIControlStateNormal];
        self.SendProvince.selected=NO;
        self.SendCity.selected=NO;
        self.SendDistrict.selected=NO;
        self.GetProvince.selected=NO;
        self.GetCity.selected=NO;
        self.GetDistrict.selected=NO;
        self.title=@"修改寄件";
    }
    // Do any additional setup after loading the view from its nib.
}
-(void)cancel
{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)SendProvinceAction:(id)sender {
    [self cancel];
    void (^animations)(NSArray *arr) = ^(NSArray *arr){
        Pickview = [[AntsPickView alloc]initPickviewWithArray:arr];
        [Pickview show:^(NSString *pSelectData,NSInteger select) {
            [self.SendProvince setTitle:pSelectData forState:UIControlStateNormal];
            SendProvinceSelect =select;
            self.SendProvince.selected=NO;
            self.SendCity.selected=YES;
            [SendCityList removeAllObjects];
            self.SendDistrict.selected=YES;
            isSendProvince=YES;
        }];
        Pickview = nil;
    };
    if (SendProvinceList.count>0) {
        NSMutableArray *title = [clsProvince GetTitleByArr:SendProvinceList];
        animations(title);
    }else{
        [clsOtherFun showLoadingView:@"正在获取数据..."];
        [ants GetProvince:^(NSDictionary *dic) {
            [clsOtherFun hideLoadingView];
            SendProvinceList = [clsProvince GetDataByArr:[dic objectForKey:@"data"]];
            NSMutableArray *title = [clsProvince GetTitleByArr:SendProvinceList];
            animations(title);
        }];
    }

}
- (IBAction)SendCityAction:(id)sender {
    [self cancel];
    void (^animations)(NSArray *arr) = ^(NSArray *arr){
        Pickview = [[AntsPickView alloc]initPickviewWithArray:arr];
        [Pickview show:^(NSString *pSelectData,NSInteger select) {
            if(![pSelectData isEqualToString:@""])
            {
            [self.SendCity setTitle:pSelectData forState:UIControlStateNormal];
            SendCitySelect =select;
            self.SendCity.selected=NO;
            [SendDistrictList removeAllObjects];
            self.SendDistrict.selected=YES;
                isSendCity=YES;
            }
        }];
        Pickview = nil;
    };
    if (SendCityList.count>0) {
        NSMutableArray *title = [clsCity GetTitleByArr:SendCityList];
        animations(title);
    }else{
        if (self.SendProvince.selected) {
            [clsOtherFun ShowMsg:@"請選擇寄件人省份"];
            return;
        }
        [clsOtherFun showLoadingView:@"正在获取数据..."];
        clsProvince *Province =[SendProvinceList objectAtIndex:SendProvinceSelect];
        [ants GetCity:Province.pid action:^(NSDictionary *dic) {
            [clsOtherFun hideLoadingView];
            SendCityList = [clsCity GetDataByArr:[dic objectForKey:@"data"]];
            NSMutableArray *title = [clsCity GetTitleByArr:SendCityList];
            animations(title);
        }];
    }

}
- (IBAction)SendDistrictAction:(id)sender {
    [self cancel];
    void (^animations)(NSArray *arr) = ^(NSArray *arr){
        Pickview = [[AntsPickView alloc]initPickviewWithArray:arr];
        [Pickview show:^(NSString *pSelectData,NSInteger select) {
            if(![pSelectData isEqualToString:@""])
            {
            [self.SendDistrict setTitle:pSelectData forState:UIControlStateNormal];
            SendDistrictSelect =select;
            self.SendDistrict.selected=NO;
                isSendDistrict=YES;
            }
        }];
        Pickview = nil;
    };
    if (SendDistrictList.count>0) {
        NSMutableArray *title = [clsDistrict GetTitleByArr:SendDistrictList];
        animations(title);
    }else{
        if (self.SendCity.selected) {
            [clsOtherFun ShowMsg:@"請選擇寄件人城市"];
            return;
        }
        [clsOtherFun showLoadingView:@"正在获取数据..."];
        clsCity *City =[SendCityList objectAtIndex:SendCitySelect];
        [ants GetDistrict:City.cid action:^(NSDictionary *dic) {
            [clsOtherFun hideLoadingView];
            SendDistrictList = [clsDistrict GetDataByArr:[dic objectForKey:@"data"]];
            NSMutableArray *title = [clsDistrict GetTitleByArr:SendDistrictList];
            animations(title);
        }];
    }
}
- (IBAction)GetProvinceAction:(id)sender {
    [self cancel];
    void (^animations)(NSArray *arr) = ^(NSArray *arr){
        Pickview = [[AntsPickView alloc]initPickviewWithArray:arr];
        [Pickview show:^(NSString *pSelectData,NSInteger select) {
            [self.GetProvince setTitle:pSelectData forState:UIControlStateNormal];
            GetProvinceSelect =select;
            self.GetProvince.selected=NO;
            self.GetCity.selected=YES;
            [GetCityList removeAllObjects];
            self.GetDistrict.selected=YES;
            isGetProvince=YES;
        }];
        Pickview = nil;
    };
    if (GetProvinceList.count>0) {
        NSMutableArray *title = [clsProvince GetTitleByArr:GetProvinceList];
        animations(title);
    }else{
        [clsOtherFun showLoadingView:@"正在获取数据..."];
        [ants GetProvince:^(NSDictionary *dic) {
            [clsOtherFun hideLoadingView];
            GetProvinceList = [clsProvince GetDataByArr:[dic objectForKey:@"data"]];
            NSMutableArray *title = [clsProvince GetTitleByArr:GetProvinceList];
            animations(title);
        }];
    }
}
- (IBAction)GetCityAction:(id)sender {
    [self cancel];
   
    void (^animations)(NSArray *arr) = ^(NSArray *arr){
        if (!arr.count>0) {
            [clsOtherFun ShowMsg:@""];
            return;
        }
        Pickview = [[AntsPickView alloc]initPickviewWithArray:arr];
        [Pickview show:^(NSString *pSelectData,NSInteger select) {
            if(![pSelectData isEqualToString:@""])
            {
            [self.GetCity setTitle:pSelectData forState:UIControlStateNormal];
            GetCitySelect =select;
            self.GetCity.selected=NO;
            [GetDistrictList removeAllObjects];
            self.GetDistrict.selected=YES;
                isGetCity=YES;
            }
        }];
        Pickview = nil;
    };
    if (GetCityList.count>0) {
        NSMutableArray *title = [clsCity GetTitleByArr:GetCityList];
        animations(title);
    }else{
        if (self.GetProvince.selected) {
            [clsOtherFun ShowMsg:@"請選擇收件人省份"];
            return;
        }
        [clsOtherFun showLoadingView:@"正在获取数据..."];
        clsProvince *Province =[GetProvinceList objectAtIndex:GetProvinceSelect];
        [ants GetCity:Province.pid action:^(NSDictionary *dic) {
            [clsOtherFun hideLoadingView];
            GetCityList = [clsCity GetDataByArr:[dic objectForKey:@"data"]];
            NSMutableArray *title = [clsCity GetTitleByArr:GetCityList];
            animations(title);
        }];
    }

}
- (IBAction)GetDistrictAction:(id)sender {
    [self cancel];
    void (^animations)(NSArray *arr) = ^(NSArray *arr){
        Pickview = [[AntsPickView alloc]initPickviewWithArray:arr];
        [Pickview show:^(NSString *pSelectData,NSInteger select) {
            if(![pSelectData isEqualToString:@""])
            {
            [self.GetDistrict setTitle:pSelectData forState:UIControlStateNormal];
            GetDistrictSelect =select;
            self.GetDistrict.selected=NO;
                isGetDistrict=YES;
            }
        }];
        Pickview = nil;
    };
    if (GetDistrictList.count>0) {
        NSMutableArray *title = [clsDistrict GetTitleByArr:GetDistrictList];
        animations(title);
    }else{
        if (self.GetCity.selected) {
            [clsOtherFun ShowMsg:@"請選擇收件人城市"];
            return;
        }
        clsCity *City =[GetCityList objectAtIndex:GetCitySelect];
        [clsOtherFun showLoadingView:@"正在获取数据..."];
        [ants GetDistrict:City.cid action:^(NSDictionary *dic) {
            [clsOtherFun hideLoadingView];
            GetDistrictList = [clsDistrict GetDataByArr:[dic objectForKey:@"data"]];
            NSMutableArray *title = [clsDistrict GetTitleByArr:GetDistrictList];
            animations(title);
        }];
    }
}
- (IBAction)ISBJAction:(id)sender {
    self.getSure.selected=!self.getSure.selected;
}
- (IBAction)CancelAction:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"是否取消？" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
    [alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (IBAction)SureAction:(id)sender {
    [self Cancel];
    if ([[clsOtherFun getName]isEqualToString:@""]) {
        [clsOtherFun ShowMsg:@"請先登錄!"];
        return;
    }
    NSString *send = [self.send text];
    if ([send isEqualToString:@""]||send==nil) {
        [clsOtherFun ShowMsg:@"請輸入寄件人"];
        [self.send becomeFirstResponder];
        return;
    }
    NSString *sendNo = [self.sendNo text];
    if ([sendNo isEqualToString:@""]||sendNo==nil) {
        [clsOtherFun ShowMsg:@"請輸入寄件人電話"];
        [self.sendNo becomeFirstResponder];
        return;
    }
    if (self.SendProvince.selected) {
        [clsOtherFun ShowMsg:@"請選擇寄件人省份"];
        return;
    }
    if (self.SendCity.selected) {
        [clsOtherFun ShowMsg:@"請選擇寄件人城市"];
        return;
    }
    if (self.SendDistrict.selected) {
        [clsOtherFun ShowMsg:@"請選擇寄件人區域"];
        return;
    }
    NSString *sendAdd = [self.sendAdd text];
    if ([sendAdd isEqualToString:@""]||sendAdd==nil) {
        [clsOtherFun ShowMsg:@"請輸入寄件人詳細地址"];
        [self.sendAdd becomeFirstResponder];
        return;
    }
    NSString *get = [self.get text];
    if ([get isEqualToString:@""]||get==nil) {
        [clsOtherFun ShowMsg:@"請輸入收件人"];
        [self.get becomeFirstResponder];
        return;
    }
    NSString *getNo = [self.getNo text];
    if ([getNo isEqualToString:@""]||getNo==nil) {
        [clsOtherFun ShowMsg:@"請輸入收件人電話"];
        [self.getNo becomeFirstResponder];
        return;
    }
    if (self.GetProvince.selected) {
        [clsOtherFun ShowMsg:@"請選擇收件人省份"];
        return;
    }
    if (self.GetCity.selected) {
        [clsOtherFun ShowMsg:@"請選擇收件人城市"];
        return;
    }
    if (self.GetDistrict.selected) {
        [clsOtherFun ShowMsg:@"請選擇收件人區域"];
        return;
    }
    NSString *getAdd = [self.getAdd text];
    if ([getAdd isEqualToString:@""]||getAdd==nil) {
        [clsOtherFun ShowMsg:@"請輸入收件人詳細地址"];
        [self.getAdd becomeFirstResponder];
        return;
    }
    NSString *getName = [self.getName text];
    if ([getName isEqualToString:@""]||getName==nil) {
        [clsOtherFun ShowMsg:@"請輸入托寄物名稱"];
        [self.getName becomeFirstResponder];
        return;
    }
    NSString *isbj = self.getSure.selected?@"1":@"0";
    if (self.dic) {
        
    }
    clsProvince *sendProvince =[SendProvinceList objectAtIndex:SendProvinceSelect];
    clsCity *sendCity =[SendCityList objectAtIndex:SendCitySelect];
    clsDistrict *sendDistrict = [SendDistrictList objectAtIndex:SendDistrictSelect];
    clsProvince *getProvince =[GetProvinceList objectAtIndex:GetProvinceSelect];
    clsCity *getCity =[GetCityList objectAtIndex:GetCitySelect];
    clsDistrict *getDistrict = [GetDistrictList objectAtIndex:GetDistrictSelect];
    if ([clsOtherFun CheckNetworkStatus:YES]) {
        [clsOtherFun showLoadingView:@"正在修改數據..."];
        if (self.dic) {
            [ants UpdateSendExpress:get :getNo :isGetProvince?getProvince.pid:[self.dic objectForKey:@"SEProvince"] :isGetCity?getCity.cid:[self.dic objectForKey:@"SECity"] :isGetDistrict?getDistrict.did:[self.dic objectForKey:@"SEDIstrict"] :getAdd :[clsOtherFun getMemberId] :[clsOtherFun getName] :getName :isSendProvince?sendProvince.pid:[self.dic objectForKey:@"MemberProvince"]  :isSendCity?sendCity.cid:[self.dic objectForKey:@"MemberCity"]  :isSendDistrict?sendDistrict.did:[self.dic objectForKey:@"MemberDIstrict"]  :isbj :sendAdd :[self.dic objectForKey:@"Expr12"] :sendNo];
        }else{
            [ants SaveSendExpress:get :getNo :getProvince.pid :getCity.cid :getDistrict.did :getAdd :[clsOtherFun getMemberId] :[clsOtherFun getName] :getName :sendProvince.pid :sendCity.cid :sendDistrict.did :isbj :sendAdd  :sendNo];
        }
    }
    
    
}
-(void)getAntsDataSuccess:(NSInteger)type :(NSMutableDictionary *)pDictionary
{
    [clsOtherFun hideLoadingView];
    NSDictionary *dic =[pDictionary objectForKey:@"data"];
    NSString *status = [dic objectForKey:@"status"];
    if ([status integerValue]==200) {
        [clsOtherFun ShowMsg:[dic objectForKey:@"message"]];
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [clsOtherFun ShowMsg:[dic objectForKey:@"message"]];
    }
    
}
@end
